﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Quiz.Model;
using Quiz.Player.Models;
using Quiz.Model.Repository;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Quiz.Player.Controllers
{
  
    public class PlayController : Controller
    {
        QuestionRepository _qRepository;
        public PlayController(QuestionRepository queryRepository)
        {
            try
            {
                _qRepository = queryRepository;
            }
            catch
            {
            }
        }
        public PlayController()
        {
            try
            {
                _qRepository = new QuestionRepository(AzureConfig.AzureAccount.CloudAccount.CreateCloudTableClient()); 
            }
            catch { 
            }
        }
        //
        // GET: /Play/

        public ActionResult Index()
        {
            return View(_qRepository.Questions.ToList());
        }

        //
        // GET: /Play/Details/5

        public ActionResult Details(string id = null)
        {
            Question question = _qRepository.Find(x=> x.RowKey == id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // GET: /Play/Create

        public ActionResult Create()
        {
            Question question = new Question();
            return View(question);
        }

        //
        // POST: /Play/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Question question, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                _qRepository.Add(question);
                _qRepository.SaveChanges();
                if (form["question"] != null)
                {
                    Session["QuestionID"] = question.RowKey;
                    return RedirectToAction("Index");
                }
                else
                {
                    Session["QuestionID"] = question.RowKey;
                    return RedirectToAction("Index", "Answer", new { ID=question.RowKey });
                }
            }

            return View(question);
        }

        //
        // GET: /Play/Edit/5

        public ActionResult Edit(string id = null)
        {
            Question question = _qRepository.Find(x => x.RowKey == id);

            if (question == null)
            {
                return HttpNotFound();
            }
            Session["QuestionID"] = question.RowKey;
            return View(question);
        }

        //
        // POST: /Play/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Question question, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                Session["QuestionID"] = question.RowKey;
                Question question2 = _qRepository.Find(x => x.RowKey == question.RowKey);
                if (question2 == null)
                {
                    return View(question);
                }
                question2.AnswerRowID = question.AnswerRowID;
                question2.QuestionText = question.QuestionText;
                _qRepository.Update(question2);
                if (form["save"] != null)
                {
                    Session["QuestionID"] = question.RowKey;
                    return RedirectToAction("Index");
                }
                else
                {
                    Session["QuestionID"] = question.RowKey;
                    return RedirectToAction("Index", "Answer", new { ID = question.RowKey });
                }
            }
            return View(question);
        }

        //
        // GET: /Play/Delete/5

        public ActionResult Delete(string id = null)
        {
            Question question = _qRepository.Find(x => x.RowKey == id);
            if (question == null)
            {

                return HttpNotFound();
            }
            Session["QuestionID"] = question.RowKey;
            return View(question);
        }

        //
        // POST: /Play/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            Question question = _qRepository.Find(x => x.RowKey == id);
            Session["QuestionID"] = question.RowKey;
            _qRepository.Delete(question);
            Session["QuestionID"] = null;
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _qRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}